package org.balthie.cal.impl.cmd.spec;

import java.util.Deque;

import org.balthie.cal.core.ICalKind.CalKind;
import org.balthie.cal.common.ex.InputException;
import org.balthie.cal.core.ICommand;

public class ContainsCmdKindSpecs {

	public static final ContainsCmdKindSpec CONTAINS_MATHOPT = new ContainsCmdKindSpec(CalKind.MathOperator);

	public static class ContainsCmdKindSpec extends AbsCmdsSpec {

		private CalKind expect;

		private ContainsCmdKindSpec(CalKind literallynumb) {
			this.expect = literallynumb;
		}

		@Override
		protected boolean doCheck(Deque<ICommand> candidate, boolean isThrow) {
			boolean isSatisfied = (!candidate.isEmpty()
					&& candidate.stream().filter(c -> expect == c.getCalKind()).findAny().isPresent());

			if (!isSatisfied && isThrow) {
				throw new InputException(
						String.format("ERROR: %s [%s is disappear] ", this.getClass().getSimpleName(), this.expect));
			} else {
				return isSatisfied;
			}
		}
	}
}
